import { getOriginDocument as getDocument, getOriginWindow as getWindow} from './base';

export const getOriginDocument = getDocument;
export const getOriginWindow = getWindow;
export function checkLoadEleCompelete(target, timeout = 3000) {
    return new Promise(resolve => {
        let isTimeout = false;
        let targetEle = null;
        let timmer = setTimeout(() => {
            isTimeout = true;
        }, 3000);
        try {
            let intervalTimmer = setInterval(() => {
                if(isTimeout) {
                    throw new Error('timeout')
                }
                let temp = cy.get(target);
                if(temp) {
                    targetEle = temp;
                    clearTimeout(timmer);
                    clearInterval(intervalTimmer);
                    // throw new Error('success');
                    resolve([null, targetEle]);
                }
            }, 200);
        } catch (error) {
            resolve([error, targetEle]);
        }
        // return [null, targetEle]
    });

}

export function sleep(ms = 1000) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve();
        }, ms);
    })
}

export async function triggerCheckbox(container) {
    await sleep(500);
    if(container.hasClass('ant-checkbox-wrapper')) {
        container.find('.ant-checkbox-input').trigger('click');
    } else {
        container.find('.ant-checkbox-wrapper').find('.ant-checkbox-input').trigger('click');
    }
    await sleep(1000);
}

export async function  selectUser(names) {
    cy.get('.ant-modal-root');
    await sleep(1000);
    for(let name of names) {
        cy.$$('.ant-modal-root').find('.ant-input').trigger('focus');
        cy.$$('.ant-modal-root').find('.ant-input').val('');
        getOriginDocument().execCommand('inserttext', false, name);
        await waitForSome(() => {
            try {
                let text = cy.$$('.ant-modal-root').find('.kpd-simple-table-body').find('.kpd-simple-table-row').eq(0).find('.kpd-simple-table-td').eq(2).text().trim();
                return text === name;
            } catch (error) {
                console.error(error);
                return false;
            }
        });
        let checkboxContainer = cy.$$('.ant-modal-root').find('.kpd-simple-table-body').find('.kpd-simple-table-row').eq(0).find('.kpd-simple-table-td').eq(0);
        await triggerCheckbox(checkboxContainer.eq(0));
        await sleep(3000); 
    }
    cy.$$('.ant-modal-root').find('.ant-btn-primary').trigger('click');
}
export async function scrollToVisible(container, target,idx, rowHeight = 40) {
    const height = container.height();
    if(idx * 40 > height) {
        container.scrollTop(idx * 40);
    }
}
export async function waitForSome(conditionCallback, timeout = 10000) {
    return new Promise(resolve => {
        let timmoutTimmer = null;
        let timmer = setInterval(() => {
            if(conditionCallback()) {
                clearInterval(timmer);
                clearTimeout(timmoutTimmer);
                timmer = null;
                resolve(true);
            }
        }, 300);
        timmoutTimmer = setTimeout(() => {
            if(timmer !== null) {
                clearInterval(timmer);
                
            }
            clearTimeout(timmoutTimmer);
            resolve(false);
        }, timeout);
    });
}